<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业实现低成本数字化转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\validate\energy;

use app\common\repositories\property\basic\PropertyBasicRepository;
use think\Validate;

class EnergyPlanValidate extends Validate
{
    protected $rule = [
        'property_id|所属物业' => 'require|checkPropertyData',
        'mode_type|方案类型' => 'require|in:1,2,3',
        'plan_name|方案呢称' => 'require',
        'service_switch|服务费状态' => 'require|in:1,2',
        'loss_switch|损耗费状态' => 'require|in:1,2',
    ];

    public function sceneAdd()
    {
        return $this->remove('id', 'require');
    }

    public function sceneEdit()
    {
        return $this->remove('device_name', 'require');
    }


    /**
     * 验证物业数据是否存在
     * @param $value
     * @return string|true
     * @throws \think\db\exception\DbException
     *
     * @author Amos
     * 2024-05-18 16:10
     */
    protected function checkPropertyData($value)
    {
        /** @var PropertyBasicRepository $propertyBasicRepository */
        $propertyBasicRepository = app()->make(PropertyBasicRepository::class);
        $exists = $propertyBasicRepository->getSearch([])->where('id', $value)->count() > 0;
        if (!$exists) {
            return '物业信息不存在';
        }
        return true;
    }
}